﻿using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Threading;
using System.Configuration;

namespace ListCreator
{
    internal class ListCreator
    {
        string sPrefix = @"";

        internal void frmCreateList_Load(object sender, ListProcessingEventArgs e)
        {
            string clipbrdText = "";
            string[] arrEachRec;

            if (Form.ModifierKeys == Keys.Control)
                sPrefix = ConfigurationManager.AppSettings["DefaultPrefix"];
            else if (Form.ModifierKeys == Keys.Alt)
            {
                sPrefix = ConfigurationManager.AppSettings["AlternatePrefix"];
                sPrefix = Interaction.InputBox("Enter domain/prefix..", @"Domain/Prefix input", sPrefix);
            }

            try
            {
                clipbrdText = Clipboard.GetText().Trim("\r\n".ToCharArray());
                arrEachRec = clipbrdText.Split("\r\n".ToCharArray());

                if (Form.ModifierKeys == Keys.Control) // Sort
                    Array.Sort(arrEachRec);

                clipbrdText = @"";
                foreach (string eachRec in arrEachRec)
                { CreateList(ref clipbrdText, eachRec, arrEachRec, e); }

                clipbrdText = @"( " + clipbrdText.Trim() + @" )";

                Clipboard.SetText(clipbrdText, TextDataFormat.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured: " + ex.Message, @"Error!");
            }
        }

        /// <summary>
        /// Creates a list of comma-separated values from a column of values
        /// </summary>
        /// <param name="sFinal">Final string containing list</param>
        /// <param name="sEachRec">each record to be added</param>
        /// <remarks></remarks>
        private void CreateList(ref string sFinal, string sEachRec, string[] arrEachRec, ListProcessingEventArgs e)
        {
            if (!String.IsNullOrEmpty(sEachRec) && !Contains(sFinal, sEachRec))
            {
                if (sFinal != @"") sFinal += @", ";

                string tmp = @"";
                if (sPrefix.Contains("%s")) tmp = sPrefix.Replace("%s", sEachRec.Trim().Trim("\t".ToCharArray()).Replace("'", "''"));
                else tmp = sPrefix + sEachRec.Trim().Trim("\t".ToCharArray()).Replace("'", "''");

                sFinal += "'" + tmp + "'";
                e.ProcessedCount++;
                Thread.Sleep(10);
            }
        }

        /// <summary>
        /// Determines if the current record is already added in the list
        /// </summary>
        /// <param name="sFinal">Final list</param>
        /// <param name="val">Value to check for</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private bool Contains(string sFinal, string val)
        {
            bool res = false;
            foreach (string str in sFinal.Split(",".ToCharArray()))
                if (str.Trim().Trim("'".ToCharArray()) == val) res = true;

            return res;
        }
    }
}
